package com.example.young.peopleaccommodationms;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

public class FragmentTest extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_test);

        Button button = (Button) findViewById(R.id.fragment_left_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //动态添加fragment
                AnotherRightFragment fragment = new AnotherRightFragment();
                FragmentManager fragmentManager = getFragmentManager();
//                AnotherRightFragment fragment = (AnotherRightFragment) fragmentManager.findFragmentById(R.layout.another_right_fragment);
//                RightFragment rightFragment = (RightFragment) fragmentManager.findFragmentById(R.layout.right_fragment);
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.right_layout, fragment);
//              提交事务之前，保存上一次的状态，便于返回上次页面。主要是模拟了返回栈的实现.具体实现过程中并没有达到目的，暂时搁置该问题
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
                AnotherRightFragment anotheFragment = (AnotherRightFragment) fragmentManager.findFragmentById(R.layout.another_right_fragment);
            }
        });
    }

}
